import 'dart:isolate';

import 'package:flutter/foundation.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'isolate_test_state.dart';

class IsolateTestController extends GetxController {
  final IsolateTestState state = IsolateTestState();

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  Future<void> startIsolate1() async {
    print('方式1：开始-->');
    EasyLoading.show();
    int result = await Isolate.run<int>((){
      return calc1(1000000000);
    });
    EasyLoading.dismiss();
    EasyLoading.showSuccess("成功");
    print('方式1：结束-->$result');

  }


  Future<void> startIsolate2() async {
    print('方式2：开始-->');
    EasyLoading.show();
    // 方法名,方法的参数，只支持1个参数
    int result = await compute(calc1, 1000000001);
    EasyLoading.dismiss();
    EasyLoading.showSuccess("成功");
    print('方式2：结束-->$result');

  }

  static int calc1(int num){
    int count = 0;
    while (num > 0) {
      if (num % 2 == 0) {
        count++;
      }
      num--;
    }
    return count;
  }
}
